//
//  XMLLoginDelegate.m
//  Seizure
//
//  Created by student on 2/28/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "XMLLoginDelegate.h"

@implementation XMLLoginDelegate

@synthesize currentElementValue;
@synthesize createdAccount;

typedef enum {
    blank,
    name,
    email,
    apns,
    type,
    sensitivity
} CurrentElement;

static NSString *xName = @"name",
                *xEmail = @"email",
                *xApns = @"apns",
                *xType = @"type",
                *xSensitivity = @"sensitivity";
CurrentElement state = blank;

- (id) init{
    
    if (self = [super init]){
        createdAccount = [[Account alloc] initWithName:@"" email:@"" isPatient:NO apnsKey:@"" sensitivity:[[NSNumber alloc] initWithDouble:50.0] ];
        currentElementValue = [[NSMutableString alloc] init ];
    }
    return self;
}

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    // info was wrong, xml returned nothing
    if ([[createdAccount email] isEqualToString:@""]){
        createdAccount = nil;
    }
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:xName])
    {
        state = name;
    }
    if ([elementName isEqualToString:xEmail]){
        state = email;
    }
    if ([elementName isEqualToString:xApns]){
        state = apns;
    }
    if ([elementName isEqualToString:xType]){
        state = type;
    }
    if ([elementName isEqualToString:xSensitivity]){
        state = sensitivity;
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:xName])
    {
//        NSLog(@"Name set to %@", currentElementValue);
        [createdAccount setName:[currentElementValue copy]];
        state = blank;
    }
    else if ([elementName isEqualToString:xEmail]){
//         NSLog(@"Email set to %@", currentElementValue);
        [createdAccount setEmail:[currentElementValue copy]];
        state = blank;
    }
    else if ([elementName isEqualToString:xApns]){
//         NSLog(@"Apns set to %@", currentElementValue);
        [createdAccount setApnsKey:[currentElementValue copy]];
        state = blank;
    }
    else if ([elementName isEqualToString:xType]){
//         NSLog(@"Type set to %@", currentElementValue);
        [createdAccount setIsPatient: [currentElementValue isEqualToString:@"1"] ? YES : NO];
        state = blank;
    }
    else if ([elementName isEqualToString:xSensitivity]){
//         NSLog(@"Sensitivity set to %@", currentElementValue);
        [createdAccount setSensitivity: [currentElementValue copy]];
        state = blank;
    }
    [currentElementValue setString:@""];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (state != blank)
    {
        [currentElementValue appendString:string];
    }
}

@end
